<?php
namespace Config;

class Wrapper {
    /**
     * Container data
     *
     * @var array $data
     */
    private $data;

    /**
     * Get container value by key
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key) {
        if (!array_key_exists($key, $this->data)) {
            $this->data[$key] = array();
        }

        $ret = & $this->data[$key];

        if (is_array($ret)) {
            $ret =& new self($ret);
        } else {
            if (preg_match('#^\[(.*)\]$#', $ret, $m)) {
                return explode(',', $m[1]);
            }
        }
        return $ret;
    }

    /**
     * @param $key
     * @param $value
     */
    public function __set($key, $value) {
        $this->data[$key] = $value;
    }

    /**
     * @param $data
     */
    public function __construct(&$data) {
        $this->data = & $data;
    }

    /**
     * @return mixed|string
     */
    public function __toString() {
        if (!$this->data) {
            return 'null';
        }

        return var_export($this->data, true);
    }
}